package com.finance.model.bankCard;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finance.model.bankCard.dto.AuthenticatedBankCardRequestDto;
import com.finance.model.bankCard.dto.AuthenticatedBankCardResponseDto;
import com.finance.pojo.BorrowerInfo;
import com.finance.pojo.CertificationInfo;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: Apollo
 * @Date: 2021/4/17
 * @Description:
 */
@Service
public class BackCard {

    @Resource
    private OkHttpClient okHttpClient;
  //  @Resource
 //   private CertificationInfoService certificationInfoService;

//    private static OkHttpClient okHttpClient = new OkHttpClient();







    //认证银行卡
    public AuthenticatedBankCardResponseDto Authenticated(BorrowerInfo borrowerInfo) throws IOException {
        //1:创建一个http客户端对象
        //OkHttpClient okHttpClient = new OkHttpClient();
        //设置参数
        AuthenticatedBankCardRequestDto authenticatedBankCardRequestDto = new AuthenticatedBankCardRequestDto();
        QueryWrapper<CertificationInfo> certificationInfoQueryWrapper = new QueryWrapper<>();
        certificationInfoQueryWrapper.lambda().eq(CertificationInfo::getBorrowerInfoCode,borrowerInfo.getCode());
      //  CertificationInfo certificationInfo = certificationInfoService.getOne(certificationInfoQueryWrapper);
        authenticatedBankCardRequestDto.setBank(borrowerInfo.getBankCardInfo().getCardNumber());
        authenticatedBankCardRequestDto.setMobile(borrowerInfo.getBankCardInfo().getPhone());
   //     authenticatedBankCardRequestDto.setName(certificationInfo.getName());
      //  authenticatedBankCardRequestDto.setNumber(certificationInfo.getIdNumber());
        authenticatedBankCardRequestDto.setType("0");
        //2:创建请求
        //2.1:先创建请求构造器
        Request.Builder builder = new Request.Builder();
        //构造路径
        builder.get();
        builder.url(AuthenticatedBankCardRequestDto.SERVICE_URL+"?"+ AuthenticatedBankCardRequestDto.COL_BANK+"="+authenticatedBankCardRequestDto.getBank()+"&"+ AuthenticatedBankCardRequestDto.COL_MOBILE+"="+authenticatedBankCardRequestDto.getMobile()+"&"+ AuthenticatedBankCardRequestDto.COL_NAME+"="+authenticatedBankCardRequestDto.getName()+"&"+ AuthenticatedBankCardRequestDto.COL_NUMBER+"="+authenticatedBankCardRequestDto.getNumber()+"&"+ AuthenticatedBankCardRequestDto.COL_TYPE+"="+authenticatedBankCardRequestDto.getType());
        //构造http头部内容
        builder.header(AuthenticatedBankCardRequestDto.AUTHORIZATION_HEADER_KEY, AuthenticatedBankCardRequestDto.APP_CODE_PRE+ AuthenticatedBankCardRequestDto.APP_CODE);
        //获取构造出来的请求对象
        Request request = builder.build();
        //执行客户端执行请求
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        //处理json的响应
        String jsonString = response.body().string();
        AuthenticatedBankCardResponseDto authenticatedBankCardResponseDto = JSONObject.parseObject(jsonString, AuthenticatedBankCardResponseDto.class);
        System.out.println(authenticatedBankCardResponseDto);
        return authenticatedBankCardResponseDto;
    }



//    public static void main(String[] args) throws IOException {
//        //1:启动注解式的spring应用
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
//        annotationConfigApplicationContext.scan("org.wyk.dto");
//        //认证银行卡
//        Authenticated();
//    }




//    //认证银行卡
//    private void Authenticated() throws IOException {
//        //1:创建一个http客户端对象
//        //OkHttpClient okHttpClient = new OkHttpClient();
//        //设置参数
//        AuthenticatedBankCardRequestDto authenticatedBankCardRequestDto = new AuthenticatedBankCardRequestDto();
//        authenticatedBankCardRequestDto.setBank("623059131102438217");
//        authenticatedBankCardRequestDto.setMobile("18967600340");
//        authenticatedBankCardRequestDto.setName("王岩康");
//        authenticatedBankCardRequestDto.setNumber("410922200108160613");
//        authenticatedBankCardRequestDto.setType("0");
//        //2:创建请求
//        //2.1:先创建请求构造器
//        Request.Builder builder = new Request.Builder();
//        //构造路径
//        builder.get();
//        builder.url(AuthenticatedBankCardRequestDto.SERVICE_URL+"?"+AuthenticatedBankCardRequestDto.COL_BANK+"="+authenticatedBankCardRequestDto.getBank()+"&"+AuthenticatedBankCardRequestDto.COL_MOBILE+"="+authenticatedBankCardRequestDto.getMobile()+"&"+ AuthenticatedBankCardRequestDto.COL_NAME+"="+authenticatedBankCardRequestDto.getName()+"&"+AuthenticatedBankCardRequestDto.COL_NUMBER+"="+authenticatedBankCardRequestDto.getNumber()+"&"+AuthenticatedBankCardRequestDto.COL_TYPE+"="+authenticatedBankCardRequestDto.getType());
//        //构造http头部内容
//        builder.header(AuthenticatedBankCardRequestDto.AUTHORIZATION_HEADER_KEY,AuthenticatedBankCardRequestDto.APP_CODE_PRE+AuthenticatedBankCardRequestDto.APP_CODE);
//        //获取构造出来的请求对象
//        Request request = builder.build();
//        //执行客户端执行请求
//        Call call = okHttpClient.newCall(request);
//        Response response = call.execute();
//        //处理json的响应
//        String jsonString = response.body().string();
//        AuthenticatedBankCardResponseDto authenticatedBankCardResponseDto = JSONObject.parseObject(jsonString, AuthenticatedBankCardResponseDto.class);
//        System.out.println(authenticatedBankCardResponseDto);
//    }

}
